In this tutorial we’ll walk you through the steps to configure an SVN repository on your Stack. You’ll be able to checkout the latest changes on your local machine, make further changes, and commit them to your Stack again so everyone working on the project is up to date.

Getting Started

You should have configured a non-root sudo user. If you haven’t already, check out our Getting Started tutorial to do so.

First we’ll update our package list and install subversion

sudo apt-get update
sudo apt-get install subversion

Now that we have SVN installed on our stack, we need to create a directory in which our SVN repositories will be stored. Where you store these repositories is ultimately up to you – we’ll be putting our repositories in /usr/local/svn. Execute the following commands on your stack:


sudo mkdir /usr/local/svn
sudo mkdir /usr/local/svn/repos

Now we need to create a SVN group with appropriate permissions:

sudo groupadd svn

We’ll give the svn group ownership of our /usr/local/svn/repos folder

sudo chgrp svn /usr/local/svn/repos

we’ll also make the repos folder group writable
sudo chmod g+w /usr/local/svn/repos

finally, we need to let the filesystem know that any files and folders created in the /usr/local/svn/repos directory will also be owned by the group
sudo chmod g+s /usr/local/svn/repos

Now, lets add our current user to the svn group
sudo usermod -a -G svn $USER

So far we’ve created the SVN repository on our Stack and set appropriate group permissions to allow our current user to make changes to the repository. If there are any other users on your stack to which you wish to grant SVN permissions, run the command

sudo usermod -a -G svn USERNAME

Now let’s create a test repository to walk you through the steps of creating a concrete repository and committing changes remotely

We’ll be creating a new repository called ‘testing’ in the /usr/local/svn/repos directory by creating the ‘testing’ directory within that directory. The ‘testing’ directory will already be owned by the ‘svn’ group but we still need to make the folder group writable (so other members of the group can write to the folder). Run the following command
umask 002

This will give all new files created read/write permissions for the owner and for the group. We can now execute the command
svnadmin create /usr/local/svn/repos/testing

This will create the ‘test’ directory and all the associated metadata. Now that we’re done creating this folder, we can set the umask back so that all new files created have read/write permissions for the owner and only read permissions for the group (which is the default)
umask 022

Now we have an empty repository ‘testing’ set up on our stack. Let’s checkout this repository using a user on our stack and creating a file. cd into your home directory and checkout the ‘testing’ repository.

cd ~
svn checkout file:///usr/local/svn/repos/testing
Checked out revision 0.

Now if you type ‘ls’ you’ll see that the ‘testing’ folder has been checked out into your home directory. let’s cd into that folder and create a text file we can commit.

cd testing
echo ‘Jello, World!’ > jelloworld.txt

This creates a file named jelloworld and inserts the text ‘Jello, World!’ into it. We can now add this to the files to be committed by running

svn add jelloworld.txt
A jelloworld.txt

Now we can commit it.

svn commit -m “Added a ‘Jello, World!’ file. Isn’t that creative?”

jelloworld.txt should now be in our repository. We’ll verify that that worked correctly when we checkout this repository on our local machine. But first we have to do a bit more configuration before we can remotely checkout an SVN repository

SVNSERVE

In order for you to be able to serve your SVN repositories remotely, you need to use the SVN server program called svnserve. When you create a SVN repository using svnadmin, subversion automatically creates a svnserve configuration file which will dictate how others access this repository remotely. The first thing we have to do is create a file that will tell svnserve who is allowed to checkout and commit to the repositories, as well as providing some basic authentication. Lets create and edit a file called ‘testing-auth’

sudo nano /usr/local/svn/testing-auth

The general structure of these authentication files looks as follows:

[users]
oneuser = password1
twouser = password2
threeuser = password3

for every user you want to access the repository, add them as ‘user = password’. Save and quit the file

Since these passwords are stored on the Stack in plain text, its a good idea to make it read-only by the owner.
sudo chmod 600 /usr/local/svn/testing-auth

Now we need to edit svnserve configuration file in each repository that we want to allow remote access to

sudo gedit /usr/local/svn/repos/test/conf/svnserve.conf

[general]
anon-access = none
password-db = /usr/local/svn/testing-auth
realm = Testing

‘anon-access’ tells svnserve that we do not want to allow just anyone to access our repository. By default, unauthenticated users would be allowed to checkout the repository but not commit changes. ‘password-db’ informs svnserve of the location of the file which contains all the authentication information. Finally, ‘realm’ just provides a name for the group of users in the ‘password-db’ file.

Now that we’ve set the basic settings, we can run the svnserve daemon inform it of the location of the repositories

sudo svnserve -d -r /usr/local/svn/repos

If we’ve configured everything correctly, we should be able to run svn checkout on our local machine and clone the ‘testing’ folder which contains the ‘jelloworld.txt’ file.

On your LOCAL machine, run the following command

svn checkout svn://YOUR_SERVER_IP_ADDRESS/testing –username USERNAME

You should replace YOUR_SERVER_IP_ADDRESS appropriately. You can choose any of the users you granted access to in the /usr/local/svn/testing-auth file as USERNAME, as long as you know the password.

You’ll be prompted for the password for the user that you provided. If everything went well, running ‘ls’ should show that you now have a ‘testing’ folder on your local machine.

cd testing
echo “I really like jello.” >> jelloworld.txt
svn commit -m “Did I tell you I really really like jello?”

You’re all set to create your own SVN repositories and allow access to a number of people now. As a final step, we’re going configure your Stack so that you don’t need to restart the svnserve daemon every time your server reboots.

On your stack, create and edit the file /etc/init.d/svnserve as follows

sudo nano /etc/init.d/svnserve

Paste the following script into the file, then save & close it.

#! /bin/sh
### BEGIN INIT INFO
# Provides: svnserve
# Required-Start: $local_fs $syslog $remote_fs
# Required-Stop: $local_fs $syslog $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start svnserve
### END INIT INFO

# Author: Michal Wojciechowski <[email protected]>

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC=”svnserve”
NAME=svnserve
DAEMON=/usr/bin/$NAME
DAEMON_ARGS=”-d -r /usr/local/svn/repos”
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

[ -x “$DAEMON” ] || exit 0

[ -r /etc/default/$NAME ] && . /etc/default/$NAME

. /lib/init/vars.sh

. /lib/lsb/init-functions

do_start()
{
start-stop-daemon –start –quiet –pidfile $PIDFILE –exec $DAEMON –test > /dev/null \
|| return 1
start-stop-daemon –start –quiet –pidfile $PIDFILE –exec $DAEMON — \
$DAEMON_ARGS \
|| return 2
}

do_stop()
{
start-stop-daemon –stop –quiet –retry=TERM/30/KILL/5 –pidfile $PIDFILE –name $NAME
RETVAL=”$?”
[ “$RETVAL” = 2 ] && return 2
start-stop-daemon –stop –quiet –oknodo –retry=0/30/KILL/5 –exec $DAEMON
[ “$?” = 2 ] && return 2
rm -f $PIDFILE
return “$RETVAL”
}

case “$1″ in
start)
[ “$VERBOSE” != no ] && log_daemon_msg “Starting $DESC” “$NAME”
do_start
case “$?” in
0|1) [ “$VERBOSE” != no ] && log_end_msg 0 ;;
2) [ “$VERBOSE” != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ “$VERBOSE” != no ] && log_daemon_msg “Stopping $DESC” “$NAME”
do_stop
case “$?” in
0|1) [ “$VERBOSE” != no ] && log_end_msg 0 ;;
2) [ “$VERBOSE” != no ] && log_end_msg 1 ;;
esac
;;
restart|force-reload)
log_daemon_msg “Restarting $DESC” “$NAME”
do_stop
case “$?” in
0|1)
do_start
case “$?” in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo “Usage: $SCRIPTNAME {start|stop|restart|force-reload}” >&2
exit 3
;;
esac

exit 0

We have to make this script executable

sudo chmod +x /etc/init.d/svnserve

Now we tell our filesystem that the script exists by running the following
sudo update-rc.d svnserve defaults

And we’re done. svnserver will start when your Stack powers up. If you need to start it up manually, run the following command

sudo /etc/init.d/svnserve start

Thanks for following along with this tutorial! Check out our community section for more tutorial on how to get #STACKED with Stack Harbor.